#include "aclnn_lp_norm_v2.h"
#include "utils.h"
#include <cstdint>
extern "C"
{
    int run(void *env_ptr, void *x, double pOptional, int64_t dim, bool keepdimOptional, double epsilonOptional,
            void *y)
    {
        uint64_t workspaceSize;
        aclOpExecutor *executor;
        // 调用aclnnAdd第一段接口
        aclIntArray *dim_array = nullptr;
        if (dim >= 0)
        {
            dim_array = aclCreateIntArray((const int64_t *)&dim, 1);
        }
        auto ret = aclnnLpNormV2GetWorkspaceSize(CAST_TO_ACL_TENSOR(x), pOptional, dim_array, keepdimOptional,
                                                 epsilonOptional, CAST_TO_ACL_TENSOR(y), &workspaceSize, &executor);
        CHECK_SUCCESS_RETURN(ret, "aclnnMseLossGradGetWorkspaceSize failed.");
        // 根据第一段接口计算出的workspaceSize申请device内存
        ALLOC_WORKSPACE(workspaceSize);
        std::cout << "workspace size = " << workspaceSize << std::endl;
        // 调用aclnnAddCustom第二段接口
        ret = aclnnLpNormV2(workspaceAddr, workspaceSize, executor, GET_STREAM(env_ptr));
        CHECK_SUCCESS_RETURN(ret, "aclnnMseLossGrad failed.")

        aclDestroyIntArray(dim_array);

        return ACL_SUCCESS;
    }
}